#include "updatethread.h"
//#include <QTest>

bool onList(QList<int> *list, int id)
{
    for(int i = 0; i < list->size(); i++)
    {
        qDebug() << i;
        if(list->at(i)== id)
        {
            list->removeAt(i);
            return true;
        }
    }
    return false;
}



UpdateThread::UpdateThread(QObject *p_parent, int p_id) :
    QThread(p_parent), parent(p_parent)
{
    QString url_str = "http://84.200.214.165/getUpdate.php?event_id=";
    url_str += id;
    url = QUrl(url_str);
    id = p_id;
    isDownloaded = false;
    qDebug() <<"Thread initialized with id " << id;
    QUrl tmpUrl("http://84.200.214.165/getLastEventId.php");
    http = new Http();
    connect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(getLastId(QByteArray)));
    http->getUrl(tmpUrl);
    connect(this,SIGNAL(emitDownload(int)),this,SLOT(download(int)));
    currentTimerId = 0;
    ignoreList = new QList<int>();
}

UpdateThread::~UpdateThread()
{
    quit();
    //delete http;
}

void UpdateThread::run()
{
    QString url_str = "http://84.200.214.165/getUpdate.php?event_id=";
    url_str += QString::number(id);
    http = new Http(this);
    url = QUrl(url_str);
    connect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(writeData(QByteArray)));
    http->getUrl(url);
    this->setPriority(QThread::LowestPriority);
    exec();
}

void UpdateThread::writeData(QByteArray array)
{
    bytes = array;
    emit dataReceived(bytes);
    currentTimerId++;
    timer.singleShot(intervall,this,SLOT(timerSlot()));
}

void UpdateThread::getLastId(QByteArray array)
{
    //qDebug() << "getLastId";
    QDomNodeList list = Parser::elementsByTagName(array,QString("id"));
    id = list.at(0).firstChild().nodeValue().toInt();
    disconnect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(getLastId(QByteArray)));
    http->deleteLater();
    //isDownloaded = true;
    //quit();
}

void UpdateThread::sendSecond()
{
    //qDebug() <<"secondRequest started";
    //connect(second_http,SIGNAL(dataReceived(QByteArray)),this,SLOT(writeSecondData(QByteArray)));
    //second_http->getUrl(url);
}

void UpdateThread::writeSecondData(QByteArray array)
{
    http->abort();

    bytes = array;
    emit dataReceived(bytes);
}

void UpdateThread::download(int timerId)
{
    disconnect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(writeData(QByteArray)));
    if(!onList(ignoreList,timerId))
    {
        qDebug() <<"QTimer("<<timerId<<")";
        http->deleteLater();
        disconnect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(writeData(QByteArray)));
        http = new Http();
        connect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(writeData(QByteArray)));

        QString url_str = "http://84.200.214.165/getUpdate.php?event_id=";
        url_str += QString::number(id);
        url = QUrl(url_str);
        http->getUrl(url);
    }
    else
        qDebug()<<"QTimer::ignored nr"<<timerId;

}

void UpdateThread::resetTimer()
{
    qDebug() << "QTimer::reset()";
    disconnect(http,SIGNAL(dataReceived(QByteArray)),this,SLOT(writeData(QByteArray)));
    ignoreList->append(currentTimerId);
    qDebug() << "size::"<< ignoreList->size();
    qDebug() <<"QTimer::ignore("<<currentTimerId<<")";
    currentTimerId++;
    timer.singleShot(intervall,this,SLOT(timerSlot()));
}

void UpdateThread::timerSlot()
{
    emit emitDownload(currentTimerId);
}

